<?php
class Mvolume extends CI_Model{
    
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function getVolume(){
        $this->db->select(' volume.id as volume_id, volume.no as volume_no, volume.name as volume_name, book_id');
        $this->db->from('volume');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getVolumeByLimit($number, $offset){
        $this->db->select(' volume.id as volume_id, volume.no as volume_no, volume.name as volume_name, book.name as book_name');
        $this->db->from('volume');
        $this->db->join('book'        , 'book.id = volume.book_id', 'inner');
        $this->db->order_by('volume.id', 'desc'); 
        $this->db->limit($number, $offset);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getVolumeByID($volume_id){
        $this->db->select(' volume.id as volume_id, volume.no as volume_no, volume.name as volume_name, volume.book_id as book_id, book.name as book_name ');
        $this->db->from('volume');
        $this->db->join('book'        , 'book.id = volume.book_id', 'inner');
        $this->db->where('volume.id',$volume_id);
        $query = $this->db->get();
        return $query->row_array();
    }
    
	public function getVolumeByBookID($book_id = NULL){
        $this->db->select(' id, no, name, book_id ');
        $this->db->from('volume');
        if($book_id != NULL){
        	$this->db->where('book_id',$book_id);
        }
        $query = $this->db->get();
        $volumes = array();
		if($query->result()){
	 		foreach ($query->result() as $volume) {
				$volumes[$volume->no] = $volume->name;
	 		} 
	 		return $volumes;
		}else{
 			return FALSE;
 		}
    }

    public function insertVolume($param){
        $data = array(
            'id' => NULL,
            'no' => $param['no'],
            'name' => $param['name'],
            'book_id' => $param['book_id']
        );
        $this->db->insert('volume', $data); 
    }

    public function updateVolume($param){
        $data = array(
            'no' => $param['no'],
            'name' => $param['name'],
            'book_id' => $param['book_id']
        );
        $this->db->where('id', $param['id']);
        $this->db->update('volume', $data);
    }

    public function deteleVolume($volume_id){
        $this->db->where('id', $volume_id);
        $this->db->delete('volume');
    }

    public function countVolume(){
        $this->db->select('volume.id');
        $this->db->from('volume');       
        $query = $this->db->get();
        return $query->num_rows();
    }
}
?>